package com.hu.service;

import com.hu.bookstorecommon.entity.DataPage;
import com.hu.bookstorecommon.entity.book;
import com.hu.bookstorecommon.entity.category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

@Service
public class bookServiceImpl implements bookService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${book.rest.url}")
    private  String bookUrl;


    @Value("${book.category.url}")
    private  String cateUrl;

    @Value("${page.size}")
   private  Integer pageSize;
    public DataPage<book> getAllBooks(Integer pageNum, Integer cid, String authorName, String bookName) {
        String url = String.format("%s?pageNum=%d&pageSize=%d", bookUrl, pageNum, pageSize);
        if (cid != null) {
            url += "&cid=" + cid;
        }
        if (authorName != null && !authorName.equals("")) {
            url += "&authorName=" + authorName;
        }
        if (bookName != null && !bookName.equals("")) {
            url += "&bookName=" + bookName;
        }
        //System.out.println(url);
     return restTemplate.getForObject(url,DataPage.class);


    }

    @Override
    public List<category> getAllCateGory() {

       // System.out.println(cateUrl);

                category[] category=restTemplate.getForObject(cateUrl,category[].class);
                return Arrays.asList(category) ;
    }
}
